package com.llyb.controller;

import com.llyb.service.Chatrecord;
import com.llyb.service.DateTimeTools;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ToolCallingController {

    @Resource
    private ChatModel chatModel;


    @Resource
    private ChatClient chatClient;


    // http://localhost:6013/toolcall/chat
    @GetMapping("/toolcall/chat")
    public String chat(@RequestParam(name = "msg",defaultValue = "你是谁，现在几点了,随机获取一张照片")String msg){

        ToolCallback[] tools = ToolCallbacks.from(new DateTimeTools());

        ToolCallingChatOptions options = ToolCallingChatOptions.builder().toolCallbacks(tools).build();

        Prompt prompt = new Prompt(msg, options);

        return chatModel.call(prompt).getResult().getOutput().getText();

    }

    @GetMapping("/toolcall/chat2")
    public Flux<String> chat2(@RequestParam(name = "msg",defaultValue = "你是谁，现在几点了,随机获取一张照片")String msg){


        return chatClient.prompt(msg).tools(new DateTimeTools()).stream().content();

    }

    // http://localhost:6013/toolcall/student?msg=查询性别是男生的学生
    @GetMapping("/toolcall/student")
    public Flux<String> getStudent(@RequestParam(name = "msg")String msg){

        return chatClient.prompt(msg).tools(new DateTimeTools(),new Chatrecord()).stream().content();

    }

}
